package com.luoguijia.disk.login.uitl;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class DataUtil {
    // 验证码字符集
    private static final String CHAR_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final Random RANDOM = new Random();

    /**
     * 生成指定长度的验证码文本
     */
    public static String getVfcCode(int length) {
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = RANDOM.nextInt(CHAR_SET.length());
            code.append(CHAR_SET.charAt(index));
        }
        return code.toString();
    }

    /**
     * 根据验证码文本生成图片字节数组
     */
    public static byte[] getVfcImg(String code) {
        int width = 100;
        int height = 30;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();

        // 设置背景色
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);

        // 设置字体
        g2d.setColor(Color.BLACK);
        Font font = new Font("Arial", Font.BOLD, 22);
        if (GraphicsEnvironment.isHeadless()) {
            font = new Font(Font.SANS_SERIF, Font.BOLD, 22);
        } else {
            String[] availableFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
            if (availableFonts.length > 0) {
                font = new Font(availableFonts[0], Font.BOLD, 22);
            }
        }
        g2d.setFont(font);

        // 绘制验证码文本
        int x = 5;
        for (char c : code.toCharArray()) {
            int y = height / 2 + RANDOM.nextInt(8) - 4;
            g2d.drawString(String.valueOf(c), x, y);
            x += 20;
        }

        // 绘制干扰线
        g2d.setColor(Color.LIGHT_GRAY);
        for (int i = 0; i < 3; i++) {
            int x1 = RANDOM.nextInt(width);
            int y1 = RANDOM.nextInt(height);
            int x2 = RANDOM.nextInt(width);
            int y2 = RANDOM.nextInt(height);
            g2d.drawLine(x1, y1, x2, y2);
        }

        g2d.dispose();

        // 转为字节数组
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", bos);
        } catch (IOException e) {
            e.printStackTrace();
            // 生成错误提示图片
            BufferedImage errorImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D errorG = errorImg.createGraphics();
            errorG.setColor(Color.RED);
            errorG.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
            errorG.drawString("Code Error", 5, 18);
            errorG.dispose();
            try {
                ImageIO.write(errorImg, "png", bos);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return bos.toByteArray();
    }
}